from django.db import models


class CashActivity(models.Model):
    '''提现有礼活动'''

    ACTIVITY_STATE_CHOICES = (
        (1, '未开始'),
        (2, '进行中'),
        (3, '已暂停'),
        (4, '已结束')
    )

    ACTIVITY_TYPE_CHOICES = (
        (1, '自定义面额'),
        (2, '指定面额'),
        (3, '随机面额'),
        (4, '自定义面额(共享)'),
        (5, '指定面额(共享)'),
        (6, '随机面额(共享)'),
        (7, '定制活动'),
        (8, 'API派发')
    )

    JOIN_LIMIT_CHOICES = (
        (1, '限制'),
        (0, '不限制')
    )

    IS_NEED_AUTH_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_CHECK_IDENTITY_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_PUSH_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_PSBC_CARD_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_WECHAT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_WHITE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_MERCHANT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_LIMIT_WHITE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_KEGUAN_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    activity_id = models.CharField(verbose_name='平台id', max_length=20, unique=True)
    name = models.CharField(verbose_name='活动名称', max_length=32, null=True, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', null=True, blank=True)
    available_begin_time = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    available_end_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    max_amount = models.DecimalField(verbose_name='总金额', max_digits=10, decimal_places=2, default=0)
    distributed_amount = models.DecimalField(verbose_name='派发金额', max_digits=10, decimal_places=2, default=0)
    activity_state = models.SmallIntegerField(verbose_name='活动状态', choices=ACTIVITY_STATE_CHOICES, default=1, blank=True)
    activity_type = models.SmallIntegerField(verbose_name='活动类型', choices=ACTIVITY_TYPE_CHOICES, default=1, blank=True)
    create_user = models.ForeignKey('users.User', verbose_name='创建人', on_delete=models.SET_NULL, null=True, blank=True, related_name='cashcreate_set')
    owner = models.ForeignKey('users.User', verbose_name='所属人', on_delete=models.SET_NULL, null=True, blank=True, related_name='cashowner_set')
    take_amount = models.DecimalField(verbose_name='占用金额', max_digits=10, decimal_places=2, default=0)
    remain_amount = models.DecimalField(verbose_name='剩余金额', max_digits=10, decimal_places=2, default=0)
    activity_description = models.TextField(verbose_name='活动使用说明', null=True, blank=True)
    max_distributed_amount = models.DecimalField(verbose_name='最大派发金额', max_digits=10, decimal_places=2, default=0)
    min_distributed_amount = models.DecimalField(verbose_name='最小派发金额', max_digits=10, decimal_places=2, default=0)
    max_amount_by_day = models.DecimalField(verbose_name='单天发放上限金额', max_digits=10, decimal_places=2, default=0, blank=True)
    max_amount_by_user_by_day = models.DecimalField(verbose_name='客户经理单天发放上限金额', max_digits=10, decimal_places=2, default=0, blank=True)
    max_amount_by_user = models.DecimalField(verbose_name='客户经理累计发放上限金额', max_digits=10, decimal_places=2, default=0, blank=True)
    max_amount_by_customeruser = models.DecimalField(verbose_name='用户累计领取上限金额', max_digits=10, decimal_places=2, default=0, blank=True)
    is_max_amount_by_day = models.SmallIntegerField(verbose_name='单天发放上限金额限制', choices=JOIN_LIMIT_CHOICES, default=0, blank=True)
    is_max_amount_by_user_by_day = models.SmallIntegerField(verbose_name='客户经理单天发放上限金额限制', choices=JOIN_LIMIT_CHOICES, default=0, blank=True)
    is_max_amount_by_user = models.SmallIntegerField(verbose_name='客户经理累计发放上限金额限制', choices=JOIN_LIMIT_CHOICES, default=0, blank=True)
    is_max_amount_by_customeruser = models.SmallIntegerField(verbose_name='用户累计领取上限金额限制', choices=JOIN_LIMIT_CHOICES, default=0, blank=True)
    is_need_auth = models.SmallIntegerField(verbose_name='是否需要认证', choices=IS_NEED_AUTH_CHOICES, default=1, blank=True)
    is_check_identity = models.SmallIntegerField(verbose_name='是否限制内部员工', choices=IS_CHECK_IDENTITY_CHOICES, default=1)
    wx_channel = models.ForeignKey('activities.Channel', verbose_name='微信派发平台', on_delete=models.SET_NULL, null=True, blank=True, related_name='wx_channel_set')
    ali_channel = models.ForeignKey('activities.Channel', verbose_name='支付宝派发平台', on_delete=models.SET_NULL, null=True, blank=True, related_name='ali_channel_set')
    is_push_ylx = models.SmallIntegerField(verbose_name='是否需要推送邮乐享', choices=IS_PUSH_CHOICES, null=True, blank=True)
    is_psbc_card = models.SmallIntegerField(verbose_name='是否绑定邮储卡', choices=IS_PSBC_CARD_CHOICES, default=0)
    is_wechat = models.SmallIntegerField(verbose_name='是否限制微信领取', choices=IS_WECHAT_CHOICES, default=0)
    is_white = models.SmallIntegerField(verbose_name='是否白名单派发', choices=IS_WHITE_CHOICES, default=0, blank=True)
    is_merchant = models.SmallIntegerField(verbose_name='是否商家派发', choices=IS_MERCHANT_CHOICES, default=0, blank=True)
    is_white_merchant = models.SmallIntegerField(verbose_name='白名单是否关联商户号', choices=IS_MERCHANT_CHOICES, default=0, blank=True)
    white_accout = models.IntegerField(verbose_name='添加白名单商户限制', default=0)
    is_limit_white = models.SmallIntegerField(verbose_name='是否允许客户经理添加', choices=IS_LIMIT_WHITE_CHOICES, default=0)
    repeat_business_no_count = models.IntegerField(verbose_name='白名单商户允许重复数', default=1)
    is_keguan = models.SmallIntegerField(verbose_name='是否推送客管系统', choices=IS_KEGUAN_CHOICES, default=0)
    pushEventIds = models.CharField(verbose_name='营销事件ID', max_length=256, null=True, blank=True)

    class Meta:
        db_table = 'tb_cash_activity'
        verbose_name = '提现有礼活动'
        verbose_name_plural = verbose_name


class CashActivityWhiteTable(models.Model):
    '''活动参与白名单'''

    IS_ADMIN_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    mobile = models.CharField(max_length=11, verbose_name='手机号')
    cashactivity = models.ForeignKey('cashbonus.CashActivity', verbose_name='活动', on_delete=models.CASCADE)
    level = models.ForeignKey('users.Level', verbose_name='机构', on_delete=models.SET_NULL, null=True, blank=True)
    add_user = models.ForeignKey('users.User', verbose_name='添加人', on_delete=models.SET_NULL, null=True, blank=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)
    is_admin = models.SmallIntegerField(verbose_name='是否管理员添加', choices=IS_ADMIN_CHOICES, default=0)
    business_no = models.CharField(max_length=10, verbose_name='微邮付商户号', null=True, blank=True)
    contact_name = models.CharField(verbose_name='联系人姓名', max_length=100, null=True, blank=True)
    id_card_number = models.CharField(verbose_name='联系人证件号码', max_length=20, null=True, blank=True)

    class Meta:
        db_table = "tb_cash_activity_white_table"
        verbose_name = '活动参与白名单'
        verbose_name_plural = verbose_name


class CashActivityDataWhiteTable(models.Model):
    '''领奖白名单关联表'''

    IS_WHITE_TABLE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    activitydata = models.OneToOneField('cashbonus.CashActivityData', verbose_name='领奖详情', on_delete=models.CASCADE)
    business_no = models.CharField(max_length=10, verbose_name='微邮付商户号', null=True, blank=True)
    contact_name = models.CharField(verbose_name='联系人姓名', max_length=100, null=True, blank=True)
    id_card_number = models.CharField(verbose_name='联系人证件号码', max_length=20, null=True, blank=True)
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    is_white_table = models.SmallIntegerField(verbose_name='是否白名单', choices=IS_WHITE_TABLE_CHOICES, default=0)

    class Meta:
        db_table = "tb_cash_activity_data_white_table"
        verbose_name = '领奖白名单关联表'
        verbose_name_plural = verbose_name


class CashActivityData(models.Model):
    '''提现有礼领取明细'''

    STATE_CHOICES = (
        (1, '待领取'),
        (2, '已领取'),
        (3, '领取失败'),
        (4, '已撤销'),
        (5, '领取中')
    )

    IS_PUSH_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    amount = models.DecimalField(verbose_name='金额', max_digits=10, decimal_places=2, default=0)
    create_time = models.DateTimeField(verbose_name='创建时间')
    received_time = models.DateTimeField(verbose_name='领取时间', null=True, blank=True)
    cash_activity = models.ForeignKey('cashbonus.CashActivity', verbose_name='活动', on_delete=models.CASCADE)
    customer_user = models.ForeignKey('users.CustomerUser', verbose_name='领取用户', on_delete=models.SET_NULL, null=True, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    state = models.SmallIntegerField(verbose_name='领取状态', choices=STATE_CHOICES, default=1, blank=True)
    companyuser = models.ForeignKey('users.User', verbose_name='派发客户经理', on_delete=models.SET_NULL, null=True, blank=True)
    cash_activity_level = models.ForeignKey('cashbonus.CashActivityLevel', verbose_name='活动分配记录', on_delete=models.SET_NULL, null=True, blank=True)
    common_url = models.CharField(verbose_name='链接', max_length=128, null=True, blank=True)
    common_qrcode = models.ImageField(verbose_name='普码', null=True, blank=True)
    identify = models.CharField(verbose_name='领取标识', max_length=32, null=True, blank=True)
    expired_time = models.DateTimeField(verbose_name='过期时间', null=True, blank=True)
    payment_no = models.CharField(verbose_name='支付订单号', max_length=64, null=True, blank=True)
    desc = models.CharField(verbose_name='备注', max_length=100)
    nonce_str = models.CharField(verbose_name='随机字符串', max_length=32)
    return_msg = models.TextField(null=True, blank=True)
    partner_trade_no = models.CharField(verbose_name='商户订单号', max_length=32, null=True, blank=True)
    mchid = models.CharField(verbose_name='派发平台商户号', max_length=16, null=True, blank=True)
    task_id = models.CharField(verbose_name='派发平台商户号', max_length=64, null=True, blank=True)
    cash_activity_rule = models.ForeignKey('cashbonus.CashActivityRule', verbose_name='派发规则', on_delete=models.SET_NULL, null=True, blank=True)
    payeeFullName = models.CharField(verbose_name='支付宝付款信息', max_length=32, null=True, blank=True)
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='领取用户', on_delete=models.SET_NULL, null=True, blank=True)
    mobile = models.CharField(max_length=11, verbose_name='手机号', null=True, blank=True)
    login_id = models.CharField(verbose_name='支付宝账号', max_length=32, null=True, blank=True)
    realname = models.CharField(verbose_name='支付宝账号实名', max_length=32, null=True, blank=True)
    openid = models.CharField(verbose_name='微信openid', max_length=32, null=True, blank=True)
    appid = models.CharField(verbose_name='微信appid', max_length=32, null=True, blank=True)
    ali_user_id = models.CharField(verbose_name='支付宝user_id', max_length=32, null=True, blank=True)
    out_biz_no = models.CharField(verbose_name='订单号', max_length=64, null=True, blank=True)
    notify_url = models.CharField(verbose_name='回调url', max_length=256, null=True, blank=True)
    channel = models.ForeignKey('activities.Channel', verbose_name='派发平台', on_delete=models.SET_NULL, null=True, blank=True)
    is_push_ylx = models.SmallIntegerField(verbose_name='是否需要推送邮乐享', choices=IS_PUSH_CHOICES, null=True, blank=True)
    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='所属用户', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_cash_activity_data'
        verbose_name = '提现有礼领取明细'
        verbose_name_plural = verbose_name


class CashActivityDataKeguan(models.Model):
    '''推送客管系统数据'''

    PUST_STATUS_CHOICES = (
        (1, '成功'),
        (2, '失败')
    )

    cashactivitydata = models.OneToOneField('cashbonus.CashActivityData', verbose_name='派发数据', on_delete=models.CASCADE)
    push_status = models.SmallIntegerField(verbose_name='推送状态', choices=PUST_STATUS_CHOICES, default=1)
    push_time = models.DateTimeField(verbose_name='推送时间')
    return_msg = models.TextField(null=True, blank=True)

    class Meta:
        db_table = 'tb_cash_activity_data_keguan'
        verbose_name = '推送客管系统数据'
        verbose_name_plural = verbose_name


class CashActivityKeguanRecords(models.Model):
    '''推送客管系统数据操作记录'''

    OPERATE_TYPE_CHOICES = (
        (1, '开启'),
        (2, '关闭')
    )

    cashactivity = models.ForeignKey('cashbonus.CashActivity', verbose_name='活动', on_delete=models.CASCADE)
    operate_time = models.DateTimeField(verbose_name='操作时间', auto_now_add=True)
    operate_user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.CASCADE)
    operate_type = models.SmallIntegerField(verbose_name='操作类型', choices=OPERATE_TYPE_CHOICES, default=1)

    class Meta:
        db_table = 'tb_cash_activity_keguan_records'
        verbose_name = '推送客管系统数据操作记录'
        verbose_name_plural = verbose_name


class CashActivityKeGuanLog(models.Model):
    #微邮惠礼包-存量活动客管
    PUST_STATUS_CHOICES = (
        (1, '成功'),
        (2, '失败')
    )

    activity = models.OneToOneField('cashbonus.CashActivity', verbose_name='活动id', on_delete=models.CASCADE)
    push_status = models.SmallIntegerField(verbose_name='推送状态', choices=PUST_STATUS_CHOICES, default=1)
    push_time = models.DateTimeField(verbose_name='推送时间')
    return_msg = models.TextField(null=True, blank=True)

    class Meta:
        db_table = 'tb_cash_activity_keguan_log'
        verbose_name = "微邮惠礼包-存量活动客管记录"
        verbose_name_plural = verbose_name




class CashActivityLevel(models.Model):
    '''活动分配记录'''

    IS_AUTH_CHOICES = (
        (0, '无'),
        (1, '有')
    )

    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.CASCADE)
    cash_activity = models.ForeignKey('cashbonus.CashActivity', verbose_name='活动', on_delete=models.CASCADE)
    max_amount = models.DecimalField(verbose_name='总金额', max_digits=10, decimal_places=2, default=0)
    take_amount = models.DecimalField(verbose_name='占用金额', max_digits=10, decimal_places=2, default=0)
    remain_amount = models.DecimalField(verbose_name='剩余金额', max_digits=10, decimal_places=2, default=0)
    receive_amount = models.DecimalField(verbose_name='已领取金额', max_digits=10, decimal_places=2, default=0)
    p_max_amount = models.DecimalField(verbose_name='总金额', max_digits=10, decimal_places=2, default=0)
    p_take_amount = models.DecimalField(verbose_name='占用金额', max_digits=10, decimal_places=2, default=0)
    p_remain_amount = models.DecimalField(verbose_name='剩余金额', max_digits=10, decimal_places=2, default=0)
    p_receive_amount = models.DecimalField(verbose_name='已领取金额', max_digits=10, decimal_places=2, default=0)
    is_auth = models.SmallIntegerField(verbose_name='派发权限', choices=IS_AUTH_CHOICES, default=0, blank=True)

    class Meta:
        db_table = 'tb_cash_activity_level'
        verbose_name = '活动分配记录'
        verbose_name_plural = verbose_name


class CashActivityRule(models.Model):
    '''提现有礼活动规则'''

    IS_DELETE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    cash_activity = models.ForeignKey('cashbonus.CashActivity', verbose_name='活动', on_delete=models.CASCADE)
    amount = models.DecimalField(verbose_name='金额', max_digits=10, decimal_places=2, default=0)
    is_delete = models.SmallIntegerField(verbose_name='领取状态', choices=IS_DELETE_CHOICES, default=0, blank=True)
    probability = models.DecimalField(verbose_name='概率', max_digits=10, decimal_places=0, default=0)

    class Meta:
        db_table = 'tb_cash_activity_rule'
        verbose_name = '提现有礼活动'
        verbose_name_plural = verbose_name


class CashActivityLevelRelation(models.Model):
    '''活动上下级'''

    parent = models.ForeignKey('cashbonus.CashActivityLevel', verbose_name='上级', on_delete=models.CASCADE, related_name='parent_set')
    child = models.ForeignKey('cashbonus.CashActivityLevel', verbose_name='下级', on_delete=models.CASCADE, related_name='child_set')

    class Meta:
        db_table = 'tb_cash_activity_level_relation'
        verbose_name = '活动上下级'
        verbose_name_plural = verbose_name


class CashActivityLevelRecord(models.Model):
    '''活动分配操作记录'''

    OPERATION_TYPE_CHOICES = (
        (1, '超管分配增加'),
        (2, '超管分配减少'),
        (3, '管理员分配增加'),
        (4, '管理员分配减少'),
        (5, '生成派发码减少'),
        (6, '派发码过期/撤销/领取失败增加'),
        (7, '增加活动预算'),
        (8, '减少活动预算')
    )

    ACTIVITY_TYPE_CHOICES = (
        (1, '机构'),
        (2, '活动')
    )

    activity_level = models.ForeignKey('cashbonus.CashActivityLevel', verbose_name='活动分配记录', on_delete=models.SET_NULL, null=True, blank=True)
    activity = models.ForeignKey('cashbonus.CashActivity', verbose_name='活动分配记录', on_delete=models.SET_NULL, null=True, blank=True)
    amount = models.DecimalField(verbose_name='金额', max_digits=10, decimal_places=2, default=0)
    operation_type = models.SmallIntegerField(verbose_name='操作类型', choices=OPERATION_TYPE_CHOICES)
    activity_type = models.SmallIntegerField(verbose_name='活动类型', choices=ACTIVITY_TYPE_CHOICES, default=1, blank=True)
    remain_amount = models.DecimalField(verbose_name='剩余金额', max_digits=10, decimal_places=2, default=0)
    p_remain_amount = models.DecimalField(verbose_name='剩余金额', max_digits=10, decimal_places=2, default=0)
    max_amount = models.DecimalField(verbose_name='剩余金额', max_digits=10, decimal_places=2, default=0)
    p_max_amount = models.DecimalField(verbose_name='剩余金额', max_digits=10, decimal_places=2, default=0)
    user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.SET_NULL, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)
    remark = models.TextField(verbose_name='说明', null=True, blank=True)
    activity_data = models.ForeignKey('cashbonus.CashActivityData', verbose_name='派发二维码', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_cash_activity_level_record'
        verbose_name = '活动分配记录'
        verbose_name_plural = verbose_name


class CashActivityAPIRelation(models.Model):
    '''api派券归属'''

    create_user = models.ForeignKey('users.User', verbose_name='添加人', on_delete=models.CASCADE, related_name='a_create_user_set')
    developer = models.ForeignKey('users.User', verbose_name='开发者', on_delete=models.CASCADE, related_name='a_developer_set')
    cashactivity = models.ForeignKey(CashActivity, verbose_name='所属地市活动', on_delete=models.CASCADE)

    class Meta:
        db_table = "tb_cash_activity_api_relation"
        verbose_name = 'api派券归属'
        verbose_name_plural = verbose_name